using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using Preparation;

namespace Tester.Forms.GuiActions.Actions
{
    [XmlRoot("Accelerators")]
    public class AcceleratorCollection
    {
        [XmlIgnore]
        private List<Accelerator> _accelerators = new List<Accelerator>();

        public AcceleratorCollection()
        {
        }

        public Accelerator AcceleratorByName(string name)
        {
            foreach (Accelerator accelerator in _accelerators)
            {
                if (accelerator.Action == name)
                {
                    return accelerator;
                }
            }
            return null;
        }

        public void AddAccelerator(string action, bool alt, bool ctrl, bool shift, Keys key)
        {
            foreach (Accelerator accelerator in _accelerators)
            {
                if (accelerator.Action == action)
                {
                    accelerator.Action = action;
                    accelerator.Alt = alt;
                    accelerator.Ctrl = ctrl;
                    accelerator.Shift = shift;
                    accelerator.Key = key;
                    return;
                }
            }
            _accelerators.Add(new Accelerator(action, alt, ctrl, shift, key));
        }

        [XmlElement("Item",IsNullable = true)]
        public Accelerator[] Items
        {
            get
            {
                return _accelerators.ToArray();
            }
            set
            {
                _accelerators = new List<Accelerator>(value??new Accelerator[]{});
            }
        }

        public void RemoveAccelerator(string name)
        {
            foreach (Accelerator accelerator in _accelerators)
            {
                if (accelerator.Action == name)
                {
                    _accelerators.Remove(accelerator); 
                    return;
                }
            }
        }
    }

    public class Accelerator
    {
        [XmlElement]
        public string Action;
        [XmlAttribute]
        public bool Alt;
        [XmlAttribute]
        public bool Ctrl;
        [XmlAttribute]
        public bool Shift;
        [XmlAttribute]
        public Keys Key;

        public Accelerator()
        {
        }

        public Accelerator(string action, bool alt, bool ctrl, bool shift, Keys key)
        {
            Action = action; 
            Alt = alt;
            Ctrl = ctrl;
            Shift = shift;
            Key = key;
        }

        public override string ToString()
        {
            return
                (Alt ? "Alt + " : "") +
                (Ctrl ? "Control + " : "") +
                (Shift ? "Shift + " : "") +
                Key;

        }
    }

    public class AcceleratorManager
    {
        public AcceleratorCollection Accelerators;

        public AcceleratorManager()
        {
            Reload(); 
        }

        public void Dump()
        {
            string path = Application.StartupPath + @"\conf\accelerators.xml";

            try
            {
                using (TextWriter file = File.CreateText(path))
                {
                    XmlSerializer ser = new XmlSerializer(typeof(AcceleratorCollection));
                   ser.Serialize(file, Accelerators);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Errors");
                Accelerators = new AcceleratorCollection();
            }

        }

        public void Reload()
        {
            string path = Application.StartupPath + @"\conf\accelerators.xml";
            if (File.Exists(path))
            {
                try
                {
                    using (TextReader file = File.OpenText(path))
                    {
                        XmlSerializer ser = new XmlSerializer(typeof (AcceleratorCollection));
                        Accelerators = (AcceleratorCollection) ser.Deserialize(file);
                    }
                } catch (Exception e)
                {
                    MessageBox.Show(e.ToString(), "Errors");
                    Accelerators = new AcceleratorCollection();
                }
            } else
            {
                Accelerators = new AcceleratorCollection();
            }
        }
    }
}
